﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace CPPEI.WPF
{
    public delegate void ViewCloseEventHandle(View view, bool isCancel);

    public class View : ContentControl
    {
        public event ViewCloseEventHandle Close;

        /// <summary>
        /// 快照图片
        /// </summary>
        public ImageSource Snapshot { get; private set; }

        /// <summary>
        /// 视图第一次被激活
        /// </summary>
        public bool IsFirstActive { get; private set; }

        /// <summary>
        /// 视图是否处于活动状态
        /// </summary>
        public bool IsActived { get; private set; }

        private ViewState _state;

        public View()
        {
            _state = ViewState.Uninit;
            IsFirstActive = true;
            //HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            //VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            //VerticalContentAlignment = System.Windows.VerticalAlignment.Stretch;
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        public void Submit()
        {
            OnClose();
            if (Close != null)
            {
                Close(this, false);
            }
        }

        public void Cancel()
        {
            OnClose();
            if (Close != null)
            {
                Close(this, true);
            }
        }

        public void Active()
        {
            if (_state == ViewState.Actived)
            {
                return;
            }

            OnActive();
            IsFirstActive = false;
            IsActived = true;
        }

        protected virtual void OnActive()
        {
        }

        public void Deactive()
        {
            Snapshot = RenderVisaulToBitmap(this, (int)this.ActualWidth, (int)this.ActualHeight);
            if (IsActived)
            {
                OnDeactive();
                IsActived = false;
            }
        }

        /// <summary>
        /// 销毁视图
        /// </summary>
        public void Destory()
        {
            OnDestory();
        }

        protected virtual void OnDestory()
        {
        }

        protected virtual void OnDeactive()
        {
        }

        protected virtual void OnClose()
        {
        }

        internal void Freeze()
        {
            Snapshot = RenderVisaulToBitmap(this, (int)this.ActualWidth, (int)this.ActualHeight);
            OnFreeze();
        }

        protected virtual void OnFreeze()
        {
        }

        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            Debug.WriteLine("render");
            if (Snapshot == null)
            {
                Snapshot = RenderVisaulToBitmap(this, (int)this.ActualWidth, (int)this.ActualHeight);
            }
            base.OnRender(drawingContext);
        }

        System.Windows.Media.Imaging.RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height)
        {
            if (width == 0 || height == 0)
            {
                return null;
            }
            var rtb = new System.Windows.Media.Imaging.RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
            rtb.Render(vsual);

            return rtb;
        }
    }
}
